<template>
  <div class="agent-page">
    <!-- 侧边栏区域 -->
    <div class="sidebar" :class="{ collapsed: sidebarCollapsed }">
      <div class="sidebar-header">
        <div class="logo">
          <i class="icon-agent"></i>
          <span v-if="!sidebarCollapsed">AI Agents</span>
        </div>
        <div class="toggle-collapse" @click="toggleSidebar">
          <i class="icon-collapse" :class="{ rotated: sidebarCollapsed }"></i>
        </div>
      </div>

      <div class="agent-list" v-if="!sidebarCollapsed">
        <div class="search-box">
          <i class="icon-search"></i>
          <input type="text" placeholder="Search agents...">
        </div>

        <div class="agent-category">
          <div class="category-header">
            <i class="icon-category"></i>
            <span>My Agents</span>
          </div>
          <div
            v-for="agent in agents"
            :key="agent.id"
            class="agent-item"
            :class="{ active: activeAgent === agent.id }"
            @click="selectAgent(agent.id)"
          >
            <i class="icon-agent-item"></i>
            <div class="agent-info">
              <div class="agent-name">{{ agent.name }}</div>
              <div class="agent-model">{{ agent.model }}</div>
            </div>
            <div class="agent-status" :class="agent.status"></div>
          </div>
        </div>

        <div class="new-agent-btn" @click="createNewAgent">
          <i class="icon-add"></i>
          <span>Create New Agent</span>
        </div>
      </div>
    </div>

    <!-- 主内容区域 -->
    <div class="main-content" :class="{ full: sidebarCollapsed }">
      <div class="navbar-top">
        <div class="navbar-left">
          <div class="breadcrumb">AI Agents / {{ activeAgentName }}</div>
        </div>
        <div class="navbar-right">
          <div class="nav-btn">
            <i class="icon-share"></i>
            <span>Share</span>
          </div>
          <div class="nav-btn">
            <i class="icon-download"></i>
            <span>Export</span>
          </div>
          <div class="user-profile">
            <div class="avatar"></div>
            <span>Admin</span>
          </div>
        </div>
      </div>

      <div class="agent-detail">
        <div class="agent-header">
          <div class="agent-title">
            <div class="title-input">
              <input type="text" v-model="agentData.name" placeholder="Agent Name">
            </div>
            <div class="agent-meta">
              <div class="model-select">
                <i class="icon-model"></i>
                <select v-model="agentData.model">
                  <option value="gpt-4">GPT-4</option>
                  <option value="claude-2">Claude 2</option>
                  <option value="llama-2">Llama 2</option>
                </select>
              </div>
              <div class="status-indicator" :class="agentData.status">
                {{ agentData.status.charAt(0).toUpperCase() + agentData.status.slice(1) }}
              </div>
            </div>
          </div>
          <div class="agent-actions">
            <button class="btn-save" @click="saveAgent">
              <i class="icon-save"></i> Save Changes
            </button>
            <button class="btn-test">
              <i class="icon-test"></i> Test Agent
            </button>
            <button class="btn-delete" @click="deleteAgent">
              <i class="icon-delete"></i> Delete
            </button>
          </div>
        </div>

        <div class="agent-config">
          <div class="config-tabs">
            <div
              v-for="tab in tabs"
              :key="tab.id"
              class="tab"
              :class="{ active: activeTab === tab.id }"
              @click="activeTab = tab.id"
            >
              {{ tab.label }}
            </div>
          </div>

          <div class="tab-content">
            <div v-if="activeTab === 'prompt'" class="prompt-config">
              <div class="section-title">Agent Instructions</div>
              <textarea
                v-model="agentData.instructions"
                placeholder="Define how your agent should behave and what it should do..."
              ></textarea>

              <div class="section-title">Conversation Starters</div>
              <div class="starters-list">
                <div
                  v-for="(starter, index) in agentData.starters"
                  :key="index"
                  class="starter-item"
                >
                  <input type="text" v-model="agentData.starters[index]" placeholder="Example question...">
                  <i class="icon-remove" @click="removeStarter(index)"></i>
                </div>
                <div class="add-starter" @click="addStarter">
                  <i class="icon-add-small"></i>
                  <span>Add conversation starter</span>
                </div>
              </div>
            </div>

            <div v-if="activeTab === 'knowledge'" class="knowledge-config">
              <div class="section-title">Knowledge Base</div>
              <div class="knowledge-list">
                <div class="knowledge-item">
                  <div class="knowledge-info">
                    <i class="icon-file"></i>
                    <div>
                      <div class="knowledge-name">Product Manual.pdf</div>
                      <div class="knowledge-meta">Uploaded 2 days ago · 24 pages</div>
                    </div>
                  </div>
                  <i class="icon-remove"></i>
                </div>
                <div class="knowledge-item">
                  <div class="knowledge-info">
                    <i class="icon-file"></i>
                    <div>
                      <div class="knowledge-name">API Documentation.docx</div>
                      <div class="knowledge-meta">Uploaded 1 week ago · 56 pages</div>
                    </div>
                  </div>
                  <i class="icon-remove"></i>
                </div>
                <div class="add-knowledge">
                  <i class="icon-add"></i>
                  <span>Add knowledge files</span>
                </div>
              </div>

              <div class="section-title">Knowledge Settings</div>
              <div class="settings-grid">
                <div class="setting-item">
                  <label>Knowledge Retrieval</label>
                  <select v-model="agentData.knowledgeSettings.retrieval">
                    <option value="auto">Automatic</option>
                    <option value="manual">Manual</option>
                  </select>
                </div>
                <div class="setting-item">
                  <label>Relevance Threshold</label>
                  <input type="range" min="0" max="100" v-model="agentData.knowledgeSettings.threshold">
                  <span>{{ agentData.knowledgeSettings.threshold }}%</span>
                </div>
                <div class="setting-item">
                  <label>Chunk Size</label>
                  <select v-model="agentData.knowledgeSettings.chunkSize">
                    <option value="small">Small (256 tokens)</option>
                    <option value="medium">Medium (512 tokens)</option>
                    <option value="large">Large (1024 tokens)</option>
                  </select>
                </div>
              </div>
            </div>

            <div v-if="activeTab === 'advanced'" class="advanced-config">
              <div class="section-title">Model Parameters</div>
              <div class="settings-grid">
                <div class="setting-item">
                  <label>Temperature</label>
                  <input type="range" min="0" max="1" step="0.1" v-model="agentData.parameters.temperature">
                  <span>{{ agentData.parameters.temperature }}</span>
                </div>
                <div class="setting-item">
                  <label>Max Tokens</label>
                  <input type="number" v-model="agentData.parameters.maxTokens" min="100" max="4000">
                </div>
                <div class="setting-item">
                  <label>Top P</label>
                  <input type="range" min="0" max="1" step="0.1" v-model="agentData.parameters.topP">
                  <span>{{ agentData.parameters.topP }}</span>
                </div>
                <div class="setting-item">
                  <label>Frequency Penalty</label>
                  <input type="range" min="0" max="2" step="0.1" v-model="agentData.parameters.frequencyPenalty">
                  <span>{{ agentData.parameters.frequencyPenalty }}</span>
                </div>
              </div>

              <div class="section-title">Memory Settings</div>
              <div class="settings-grid">
                <div class="setting-item">
                  <label>Memory Type</label>
                  <select v-model="agentData.memory.type">
                    <option value="full">Full Context</option>
                    <option value="summary">Summary Memory</option>
                    <option value="buffer">Buffer Memory</option>
                  </select>
                </div>
                <div class="setting-item">
                  <label>Memory Duration</label>
                  <select v-model="agentData.memory.duration">
                    <option value="short">Short-term (1 hour)</option>
                    <option value="medium">Medium-term (24 hours)</option>
                    <option value="long">Long-term (7 days)</option>
                  </select>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'

// 侧边栏状态
const sidebarCollapsed = ref(false)

// 智能体数据
const agents = ref([
  {id: 1, name: 'Customer Support', model: 'GPT-4', status: 'active'},
  {id: 2, name: 'Technical Assistant', model: 'Claude 2', status: 'active'},
  {id: 3, name: 'Sales Consultant', model: 'GPT-4', status: 'draft'},
  {id: 4, name: 'Research Assistant', model: 'Llama 2', status: 'inactive'},
])

const activeAgent = ref(1)

// 当前选中的智能体数据
const agentData = reactive({
  id: 1,
  name: 'Customer Support',
  model: 'gpt-4',
  status: 'active',
  instructions: 'You are a customer support agent for a tech company. Be helpful, friendly, and concise. Answer customer questions about products and services.',
  starters: [
    'How do I reset my password?',
    'Where can I find the user manual?',
    'My order hasn\'t arrived yet',
  ],
  knowledgeSettings: {
    retrieval: 'auto',
    threshold: 75,
    chunkSize: 'medium',
  },
  parameters: {
    temperature: 0.7,
    maxTokens: 1500,
    topP: 0.9,
    frequencyPenalty: 0.5,
  },
  memory: {
    type: 'summary',
    duration: 'medium',
  },
})

// 标签页
const tabs = ref([
  {id: 'prompt', label: 'Prompt'},
  {id: 'knowledge', label: 'Knowledge'},
  {id: 'advanced', label: 'Advanced'},
])
const activeTab = ref('prompt')

// 计算属性
const activeAgentName = computed(() => {
  const agent = agents.value.find(a => a.id === activeAgent.value)
  return agent ? agent.name : 'New Agent'
})

// 方法
const toggleSidebar = () => {
  sidebarCollapsed.value = !sidebarCollapsed.value
}

const selectAgent = (id) => {
  activeAgent.value = id
  const agent = agents.value.find(a => a.id === id)
  if (agent) {
    Object.assign(agentData, {
      id: agent.id,
      name: agent.name,
      model: agent.model.toLowerCase().replace(' ', '-'),
      status: agent.status,
    })
  }
}

const createNewAgent = () => {
  const newId = Math.max(...agents.value.map(a => a.id)) + 1
  agents.value.push({
    id: newId,
    name: `New Agent ${newId}`,
    model: 'GPT-4',
    status: 'draft',
  })
  selectAgent(newId)
}

const saveAgent = () => {
  const agentIndex = agents.value.findIndex(a => a.id === agentData.id)
  if (agentIndex !== -1) {
    agents.value[agentIndex] = {
      ...agents.value[agentIndex],
      name: agentData.name,
      model: agentData.model.toUpperCase().replace('-', ' '),
      status: agentData.status,
    }
    alert('Agent saved successfully!')
  }
}

const deleteAgent = () => {
  if (confirm('Are you sure you want to delete this agent?')) {
    agents.value = agents.value.filter(a => a.id !== agentData.id)
    if (agents.value.length > 0) {
      selectAgent(agents.value[0].id)
    } else {
      createNewAgent()
    }
  }
}

const addStarter = () => {
  agentData.starters.push('')
}

const removeStarter = (index) => {
  agentData.starters.splice(index, 1)
}
</script>

<style scoped>
/* 基础变量和重置 */
:root {
  --sidebar-width: 280px;
  --sidebar-collapsed-width: 60px;
  --navbar-height: 60px;
  --primary-color: #5e35b1;
  --primary-light: #7e57c2;
  --text-primary: #2d3748;
  --text-secondary: #718096;
  --border-color: #e2e8f0;
  --bg-light: #f8f9fa;
  --bg-card: #ffffff;
  --success-color: #48bb78;
  --warning-color: #ed8936;
  --danger-color: #e53e3e;
  --info-color: #4299e1;
  --radius: 8px;
  --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
}

.agent-page {
  display: flex;
  height: 100vh;
  background-color: var(--bg-light);
  color: var(--text-primary);
}

/* 侧边栏样式 */
.sidebar {
  width: var(--sidebar-width);
  background-color: var(--bg-card);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  transition: width 0.3s ease;
  overflow: hidden;
}

.sidebar.collapsed {
  width: var(--sidebar-collapsed-width);
}

.sidebar-header {
  height: var(--navbar-height);
  display: flex;
  align-items: center;
  padding: 0 16px;
  border-bottom: 1px solid var(--border-color);
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 18px;
  font-weight: 600;
  color: var(--primary-color);
  flex-grow: 1;
}

.icon-agent {
  display: inline-block;
  width: 28px;
  height: 28px;
  background-color: var(--primary-light);
  border-radius: 6px;
  position: relative;
}

.icon-agent::before {
  content: "A";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: white;
  font-weight: bold;
}

.toggle-collapse {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.3s;
}

.toggle-collapse:hover {
  background-color: var(--bg-light);
}

.icon-collapse {
  display: inline-block;
  width: 20px;
  height: 20px;
  position: relative;
  transition: transform 0.3s;
}

.icon-collapse::before {
  content: "◀";
  position: absolute;
  top: 0;
  left: 0;
  color: var(--text-secondary);
}

.icon-collapse.rotated::before {
  content: "▶";
}

/* 智能体列表样式 */
.agent-list {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  flex-grow: 1;
  overflow-y: auto;
}

.search-box {
  position: relative;
}

.search-box input {
  width: 100%;
  padding: 10px 16px 10px 40px;
  border-radius: var(--radius);
  border: 1px solid var(--border-color);
  background-color: var(--bg-light);
  font-size: 14px;
}

.icon-search {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-secondary);
}

.agent-category {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.category-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 500;
}

.icon-category {
  display: inline-block;
  width: 16px;
  height: 16px;
  background-color: var(--text-secondary);
  border-radius: 4px;
}

.agent-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: all 0.2s;
}

.agent-item:hover {
  background-color: var(--bg-light);
}

.agent-item.active {
  background-color: rgba(94, 53, 177, 0.1);
  border-left: 3px solid var(--primary-color);
}

.icon-agent-item {
  display: inline-block;
  width: 24px;
  height: 24px;
  background-color: var(--primary-light);
  border-radius: 6px;
  position: relative;
}

.icon-agent-item::before {
  content: "A";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: white;
  font-size: 12px;
  font-weight: bold;
}

.agent-info {
  flex-grow: 1;
}

.agent-name {
  font-weight: 500;
  font-size: 14px;
}

.agent-model {
  font-size: 12px;
  color: var(--text-secondary);
}

.agent-status {
  width: 10px;
  height: 10px;
  border-radius: 50%;
}

.agent-status.active {
  background-color: var(--success-color);
}

.agent-status.draft {
  background-color: var(--warning-color);
}

.agent-status.inactive {
  background-color: var(--danger-color);
}

.new-agent-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px;
  border-radius: var(--radius);
  background-color: var(--primary-color);
  color: white;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s;
  margin-top: auto;
}

.new-agent-btn:hover {
  background-color: var(--primary-light);
}

.icon-add {
  font-weight: bold;
}

/* 主内容区域 */
.main-content {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: all 0.3s;
}

.main-content.full {
  margin-left: calc(var(--sidebar-collapsed-width) - var(--sidebar-width));
}

.navbar-top {
  height: var(--navbar-height);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 24px;
  background-color: var(--bg-card);
  border-bottom: 1px solid var(--border-color);
  box-shadow: var(--shadow);
  z-index: 10;
}

.breadcrumb {
  font-size: 14px;
  color: var(--text-secondary);
}

.navbar-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.nav-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: var(--radius);
  font-size: 14px;
  cursor: pointer;
  transition: background-color 0.2s;
}

.nav-btn:hover {
  background-color: var(--bg-light);
}

.user-profile {
  display: flex;
  align-items: center;
  gap: 8px;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background-color: var(--primary-light);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: 500;
}

/* 智能体详情区域 */
.agent-detail {
  flex-grow: 1;
  padding: 24px;
  overflow-y: auto;
}

.agent-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 24px;
  gap: 24px;
}

.agent-title {
  flex-grow: 1;
}

.title-input input {
  width: 100%;
  font-size: 24px;
  font-weight: 600;
  padding: 8px 0;
  border: none;
  border-bottom: 2px solid transparent;
  outline: none;
}

.title-input input:focus {
  border-bottom: 2px solid var(--primary-color);
}

.agent-meta {
  display: flex;
  gap: 16px;
  margin-top: 8px;
}

.model-select {
  display: flex;
  align-items: center;
  gap: 8px;
  background-color: var(--bg-light);
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 14px;
}

.icon-model {
  color: var(--text-secondary);
}

.model-select select {
  background: transparent;
  border: none;
  outline: none;
  font-size: 14px;
}

.status-indicator {
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
}

.status-indicator.active {
  background-color: rgba(72, 187, 120, 0.2);
  color: var(--success-color);
}

.status-indicator.draft {
  background-color: rgba(237, 137, 54, 0.2);
  color: var(--warning-color);
}

.agent-actions {
  display: flex;
  gap: 12px;
}

.agent-actions button {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: var(--radius);
  border: none;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-save {
  background-color: var(--primary-color);
  color: white;
}

.btn-save:hover {
  background-color: var(--primary-light);
}

.btn-test {
  background-color: var(--bg-card);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
}

.btn-test:hover {
  background-color: var(--bg-light);
}

.btn-delete {
  background-color: transparent;
  color: var(--danger-color);
}

.btn-delete:hover {
  background-color: rgba(229, 62, 62, 0.1);
}

/* 配置区域 */
.agent-config {
  background-color: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.config-tabs {
  display: flex;
  border-bottom: 1px solid var(--border-color);
}

.tab {
  padding: 16px 24px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  border-bottom: 3px solid transparent;
}

.tab:hover {
  background-color: var(--bg-light);
  color: var(--primary-color);
}

.tab.active {
  color: var(--primary-color);
  border-bottom: 3px solid var(--primary-color);
}

.tab-content {
  padding: 24px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 16px;
  color: var(--text-primary);
}

textarea {
  width: 100%;
  min-height: 150px;
  padding: 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border-color);
  margin-bottom: 24px;
  resize: vertical;
  font-size: 14px;
}

textarea:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(94, 53, 177, 0.1);
}

.starters-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.starter-item {
  display: flex;
  gap: 8px;
  align-items: center;
}

.starter-item input {
  flex-grow: 1;
  padding: 10px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border-color);
}

.icon-remove {
  color: var(--text-secondary);
  cursor: pointer;
  padding: 8px;
}

.icon-remove:hover {
  color: var(--danger-color);
}

.add-starter {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  color: var(--primary-color);
  font-weight: 500;
  cursor: pointer;
  width: fit-content;
  border-radius: var(--radius);
  transition: background-color 0.2s;
}

.add-starter:hover {
  background-color: rgba(94, 53, 177, 0.1);
}

.icon-add-small {
  font-size: 12px;
  font-weight: bold;
}

/* 知识库配置 */
.knowledge-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 24px;
}

.knowledge-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
}

.knowledge-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.icon-file {
  width: 36px;
  height: 36px;
  background-color: rgba(66, 153, 225, 0.1);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--info-color);
  font-size: 18px;
}

.knowledge-name {
  font-weight: 500;
}

.knowledge-meta {
  font-size: 12px;
  color: var(--text-secondary);
}

.add-knowledge {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border: 1px dashed var(--border-color);
  border-radius: var(--radius);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s;
}

.add-knowledge:hover {
  border-color: var(--primary-color);
  color: var(--primary-color);
  background-color: rgba(94, 53, 177, 0.05);
}

/* 设置网格 */
.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 24px;
  margin-top: 16px;
}

.setting-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.setting-item label {
  font-size: 14px;
  font-weight: 500;
}

.setting-item input[type="range"],
.setting-item select,
.setting-item input[type="number"] {
  padding: 8px 12px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  width: 100%;
}

.setting-item input[type="range"] {
  padding: 0;
}

.setting-item input[type="number"] {
  max-width: 120px;
}
</style>